package com.mymarkdown.app.util;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @author zenglun
 * @date 2021/12/5 8:55 下午
 * @description: TODO
 */
@Slf4j
public class ThreadPool {
    private static ExecutorService executorService;

    private static final int MAX_CAPACITY = 100;

    private static BlockingQueue<Runnable> WORK_QUEUE = new LinkedBlockingQueue<>(MAX_CAPACITY);
    private static  int corePoolSize = 5;

    private static  int maxImuPoolSize = 20;

    private static  long keepAliveTime = 120;

    private static ThreadPool self;


    private ThreadPool(){
        executorService = new ThreadPoolExecutor(corePoolSize, maxImuPoolSize, keepAliveTime, TimeUnit.SECONDS, WORK_QUEUE, (r, executor) -> {
            log.error("[ThreadPool]---[ThreadPool],线程池任务数已经满了!");
            throw new RejectedExecutionException("Task " + r.toString() +
                    " rejected from " +
                    executor.toString());
        });
    }

    public static ThreadPool getInstant(){
        if (self == null){
            synchronized (ThreadPool.class){
                self = new ThreadPool();
            }
        }
        return self;
    }

    public ExecutorService getExecutorService() {
        return executorService;
    }

    public void commit(Runnable task){
        executorService.execute(task);
    }
}

